<?php
namespace App\Http\Controllers\Admin;

use app\Logic\Factory;
use app\Logic\Layui\Service\AdminLogin;
use app\Logic\Layui\Service\ImageCode;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class LoginController extends Controller
{

    public function index(Request $request)
    {
        if($request->ajax()){
            return $this->loginPost($request);
        }else{

            if(session('adminId')){
                return redirect('/admin/index');
            }

            $app = Factory::Layui();
            $res = $app->VerificationCode(function ($app){
                return new ImageCode($app);
            })->image();

            return view('Admin.Login.index' , ['code' => $res->original['data']['path']]);
        }
    }


    private function loginPost($request)
    {
        $data = $request->input();

        $app = Factory::Layui();

        return $app->AdminUser(function ($app){
            return new AdminLogin($app);
        })->login($data['username'] , $data['password'] , $data['code']);
    }

}